package com.jsh.erp.controller;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.jsh.erp.service.print.PrintService;
import com.jsh.erp.utils.PrintUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/print")
public class PrintController {

    @Resource
    private PrintService printService;
    @RequestMapping(value = "/sales/order")
    public void salesOrder(@RequestParam(name = "depotHeadID",required = false)long depotHeadID, HttpServletResponse response) throws Exception {
        String exportFileName = "销售订单打印输出-" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".pdf";
        String TemplateHtmlBody = this.printService.salesOrder(depotHeadID);
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(exportFileName, "utf-8"));
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        PrintUtils.printToPDF(new Rectangle(PageSize.A4), TemplateHtmlBody, outputStream);
        outputStream.flush();
        outputStream.close();
    }

    @RequestMapping(value = "/sales/out")
    public void salesOut(@RequestParam(name = "depotHeadID",required = false)long depotHeadID, HttpServletResponse response) throws Exception {
        String exportFileName = "销售出库单打印输出-" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".pdf";
        String TemplateHtmlBody = this.printService.printSalesOut(depotHeadID);
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(exportFileName, "utf-8"));
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        PrintUtils.printToPDF(new Rectangle(595, 421), TemplateHtmlBody, outputStream);
        outputStream.flush();
        outputStream.close();
    }


    @RequestMapping(value = "/purchase/order")
    public void purchaseOrders(@RequestParam(name = "depotHeadID",required = false)long depotHeadID, HttpServletResponse response) throws Exception {
        String exportFileName = "采购订单打印输出-" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".pdf";
        String TemplateHtmlBody = this.printService.printPurchaseOrders(depotHeadID);
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(exportFileName, "utf-8"));
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        PrintUtils.printToPDF(new Rectangle(PageSize.A4), TemplateHtmlBody, outputStream);
        outputStream.flush();
        outputStream.close();
    }


    @RequestMapping(value = "/purchase/in")
    public void purchaseIn(@RequestParam(name = "depotHeadID",required = false)long depotHeadID, HttpServletResponse response) throws Exception {
        String exportFileName = "采购入库打印输出-" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".pdf";
        String TemplateHtmlBody = this.printService.purchaseIn(depotHeadID);
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(exportFileName, "utf-8"));
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        PrintUtils.printToPDF(new Rectangle(595, 421), TemplateHtmlBody, outputStream);
        outputStream.flush();
        outputStream.close();
    }
}
